package com.gljx.web.service.impl;

import com.gljx.constant.Constant;
import com.gljx.util.CommonUtil;
import com.gljx.web.Entity.Item;
import com.gljx.web.Entity.Message;
import com.gljx.web.dao.MessageMapper;
import com.gljx.web.service.MessageService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: LiXiang
 * @Description :
 * @Date: Created in 2018/2/8 10:30
 */
@Service
@Transactional

public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageMapper messageDao;

    public Map<String,Object > selectMessagePage(String rows,
                                            String page,
                                            String sort,
                                            String order,
                                            String ISLOOK,  //是否已读
                                            String messageBegin,
                                            String messageEnd,
                                            String TYPE){
        //获取当前账户session
        Session session = SecurityUtils.getSubject().getSession();

        String companyID = (String) session.getAttribute(Constant.CompanyId_session);

        String PID = Constant.Project_ID;

        String sql = "select * from  t_p_message m ";
        String whereSql = " where m.companyid = '"+companyID+"' and m.projectid = '"+PID+"' ";

        //通知 和 单据
        if("no".equals(TYPE)){
            whereSql += " and (( m.user_name = '"+(String) SecurityUtils.getSubject().getPrincipal()+"' and m.type = 1 )";
            whereSql += " or( m.type = 2 ) )";
        }
        else if("1".equals(TYPE))
        {//流程
            whereSql += " and m.user_name = '"+(String) SecurityUtils.getSubject().getPrincipal()+"' and m.type = 1 ";
        }
        else if("2".equals(TYPE))
        {//通知
            whereSql += " and  m.type = 2 ";
        }

        //已读未读
        if(!"no".equals(ISLOOK)){
                whereSql += " and m.islook = "+ISLOOK;
        }


        if(!"no".equals(messageBegin))
        {
            whereSql += " and  m.CREAT_TIME >= '"+messageBegin+"'";
        }


        if(!"no".equals(messageEnd))
        {
            whereSql += " and  m.CREAT_TIME <= '"+messageEnd+"'";
        }

        //CommonUtil.easyUI_sortToSql(sort);
        //排序
        if(!"no".equals(sort))
        {
            whereSql+=" order by m."+ CommonUtil.easyUI_sortToSql(sort)+" "+order;
        }
        else
        {
            whereSql+=" order by m.islook,m.CREAT_TIME desc";
        }


        Integer pageNum = Integer.valueOf(page);
        Integer pageSize = Integer.valueOf(rows);

        Map <String,Object > data = new HashMap<String, Object>();
        data.put("Pindex", pageNum);

        sql+= whereSql;
        data.put("Psql",sql);
        data.put("Psize", pageSize);

        Map<String, Object> stringObjectMap = messageDao.selectMessagePage(data);

        return data;
    }

    public Map<String,Object > selectItemPage(String rows,
                                              String page,
                                              String sort,
                                              String order,
                                              String ISUSE,
                                              String messageBegin,
                                              String messageEnd,
                                              String TYPE){
        //获取当前账户session
        Session session = SecurityUtils.getSubject().getSession();

        String companyID = (String) session.getAttribute(Constant.CompanyId_session);

        String PID = Constant.Project_ID;

        String sql = "select * from  T_P_ITEMS m ";
        String whereSql = " where m.companyid = '"+companyID+"' and m.projectid = '"+PID+"' ";

        //流程 和 其他
        if("no".equals(TYPE)){
            whereSql += " and  m.user_name = '"+(String) SecurityUtils.getSubject().getPrincipal()+"' ";
        }
        else if("1".equals(TYPE))
        {//流程
            whereSql += " and m.user_name = '"+(String) SecurityUtils.getSubject().getPrincipal()+"' and m.type = 1 ";
        }
        else if("2".equals(TYPE))
        {//其他
            whereSql += " and  m.type = 2 ";
        }

        //已办未办
        if(!"no".equals(ISUSE)){
            whereSql += " and m.ISUSE = "+ISUSE;
        }


        if(!"no".equals(messageBegin))
        {
            whereSql += " and  m.CREAT_TIME >= '"+messageBegin+"'";
        }


        if(!"no".equals(messageEnd))
        {
            whereSql += " and  m.CREAT_TIME <= '"+messageEnd+"'";
        }

        //CommonUtil.easyUI_sortToSql(sort);
        //排序
        if(!"no".equals(sort))
        {
            whereSql+=" order by m."+ CommonUtil.easyUI_sortToSql(sort)+" "+order;
        }
        else
        {
            whereSql+=" order by m.ISUSE,m.CREAT_TIME desc";
        }

        Integer pageNum = Integer.valueOf(page);
        Integer pageSize = Integer.valueOf(rows);

        Map <String,Object > data = new HashMap<String, Object>();
        data.put("Pindex", pageNum);

        sql+= whereSql;
        data.put("Psql",sql);
        data.put("Psize", pageSize);

        Map<String, Object> stringObjectMap = messageDao.selectItemPage(data);

        return data;
    }


    public Integer insert(Message message){

        List<Item> items = new ArrayList<Item> ();

        if(StringUtils.isEmpty(message.getUserName()))
        {
            return 0;
        }
        else 
        {
            for (int i = 0; i < message.getUserName().split(",").length; i++) {
                String userName = message.getUserName().split(",")[i];

                Item item = new Item();
                item.msgSetItem(message);
                item.setUserName(userName);
                item.setIsuse(0);//默认未读
                item.setLeavel(1);//默认一级
                items.add(item);
            }
            if(items.size() > 0)
            {
                return messageDao.insertItemList(items);
            }
        }
        Item item = new Item();
        item.msgSetItem(message);
        return messageDao.insertSelective(item);

    }

    public Message selectByPrimaryKey(Integer id){
        Message message = new Message();
        message.setId(id);
        message.setIslook(1);

        //修改为已读
        messageDao.updateByPrimaryKeySelective(message);
        return messageDao.selectByPrimaryKey(id);
    }

    @Override
    public Integer updateMessage(Integer id) {

        Message message = new Message();
        message.setId(id);
        message.setIslook(1);

        return messageDao.updateByPrimaryKeySelective(message);
    }

    @Override
    public Integer editItem(Integer id) {

        Item item = new Item();
        item.setId(id);
        item.setIsuse(1);

        return messageDao.updateItemByPrimaryKey(item);
    }

    @Override
    public Integer isUseAudit(String companyID,
                              String fnumber,
                              Integer isuse,
                              String projectID){

        return messageDao.isUseAudit(companyID, fnumber, isuse, projectID);
    }
}
